<?php
class Auth extends MY_Controller 
{
	var $language;
	private
		$cookieIndex = 'key_admins',
		
		/*
		 * stores DB row returned by cookie key
		 */
		$session = array()
	;
	
	function __construct()
	{
		$this->CI =& get_instance();				
		
		$this->CI->load->model('/site/session_model');
				
		$this->initialize();
	}
	
	function initialize()
	{
		//first user hit
		if (!isset($_COOKIE[$this->cookieIndex]))
		{
			$this->__createSession();
		}
		else
		{
			//session exists			
			if ($this->session = $this->CI->session_model->getSessionByKey($_COOKIE[$this->cookieIndex]))
			{
				if ($this->__checkSession($this->session))
				{
					//update last seen
					$item = array(
						'id' => $this->session['id'],
						'id_user' => $this->session['id_user'],
						'lastseen' => MYSQL_NOW(),
					);
					
					$this->CI->session_model->update($item);
				}
				else
				{
					//session expired or not belong user (possible haker attacks)
					$this->session = array();
					$this->__createSession();
				}
			}
			else
			{
				//session does not exist
				$this->__createSession();
			}
		}
	}
	
	function __createSession()
	{
		$session_key = randMD5();
		
		setcookie($this->cookieIndex, $session_key, time() + 60 * 60 * 24 * 365, '/');
		$_COOKIE[$this->cookieIndex] = $session_key;

		$this->CI->session_model->insert(array('key' => $session_key));
	}
	
	function __destroySession()
	{
		setcookie($this->cookieIndex, '', time() - 60 * 60 * 24 * 365, '/');
		unset($_COOKIE[$this->cookieIndex]);
	}
	
	function __checkSession($session = array())
	{
		$this->CI->load->library('input');
		
		if ($session['ipaddress'] !== $this->CI->input->ip_address())
		{
			return false;
		}

		if ($session['agnt_id'] !== $this->CI->input->user_agent())
		{
			return false;
		}

		if ($session['expire'] < MYSQL_NOW())
		{
			return false;
		}
		
		return true;
	}
	
	function is_logged_in()
	{
		if (isset($this->session['id_user']) && $this->session['id_user'] > 0)
		{
			return true;
		}
		
		return false;
	}
	
	function login($user = array())
	{
		if (!isset($user['id']))
		{
			return false;
		}

		$item = array(
			'id' => $this->session['id'],
			'id_user' => $user['id'],
			'data' => serialize($user),
		);
		
		$this->CI->session_model->update($item);

		return true;
	}
	
	function logout()
	{
		if (!count($this->session))
		{
			return false;
		}
		
		$item = array(
			'id' => $this->session['id'],
			'id_user' => null,
			'data' => '',
		);
		
		$this->CI->session_model->update($item);
		
		return true;
	}

	function getID()
	{
		if (!empty($this->session['id_user']))
		{
			return $this->session['id_user'];
		}
		else
		{
			return null;
		}
	}

	function getSessionID()
	{
		return $this->session['id'];
	}
	
	function getSessionLastSeen()
	{
		return $this->session['lastseen'];
	}

	function getUserGroup()
	{
		return $this->getUserData('role');        
	}

	/*
	function getUserName()
	{
		return $this->session['session_user_name'];
	}
	*/
		
	function getUserData($field = null)
	{
		$data = array();
		$data = @unserialize($this->session['data']);
		if (!empty($field))
		{			
			if (!empty($data[$field])) return $data[$field];
			else return '';				
		}
		else
		{
			return $data; 
		}
	}

	function setUserData($data = array())
	{
		if (!empty($this->session['id']))
		{
	        $item = array(
	            'id' => $this->session['id'],
	            'data' => serialize($data),
	        );
	
	        $this->CI->session_model->update($item);				
		}
	}

	function sessionGetValue($key = null)
	{
		if (is_null($key) || !isset($this->session[$key]))
		{
			return false;
		}
		
		return $this->session[$key];
	}

	function getSessionKey()
	{
		return $this->sessionGetValue('key');
	}	
}
?>